package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IProductTypeDao;
import com.cn.wanxi.model.ProductTypeModel;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductTypeDaoImpl implements IProductTypeDao {
    private void clearProductTypeRedis() {
        Jedis jedis = new Jedis();
        jedis.del("product_type_id");
        jedis.del("product_type_href");
        jedis.del("product_type_name");
//        jedis.del("product_type_create_time");
//        jedis.del("product_type_update_time");
//        jedis.del("product_type_enable");
//        jedis.del("product_type_note");
    }

    private ProductTypeModel getResult(ResultSet rs) throws SQLException {
        ProductTypeModel productType = new ProductTypeModel();
        productType.setId(rs.getInt("id"));
        productType.setHref(rs.getString("href"));
        productType.setName(rs.getString("name"));
        productType.setCreateTime(rs.getString("create_time"));
        productType.setUpdateTime(rs.getString("update_time"));
        productType.setEnabled(rs.getInt("enable"));
        productType.setNote(rs.getString("note"));
        return productType;
    }

    private ProductTypeModel getProductTypeResult(String sql){
        ResultSet rs = JDBCUtils.getResultSet(sql);
        ProductTypeModel productType = null;
        try {
            while (rs.next()) {
                productType = getResult(rs);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return productType;
    }
    private List<ProductTypeModel> getProductTypeListResult(String sql){
        List<ProductTypeModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()) {
                ProductTypeModel productType = getResult(rs);
                list.add(productType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public List<ProductTypeModel> getProductTypeList() {
        String sql = "select * from product_type";
        return getProductTypeListResult(sql);
    }

    @Override
    public ProductTypeModel getProductType(int id) {
        String sql = "select * from product_type where id="+id;
        return getProductTypeResult(sql);
    }

    @Override
    public int addProductType(ProductTypeModel productType) {
        String sql = "insert into product_type(name,href,create_time) values('"
                + productType.getName() +"','"+ productType.getHref()
                +"','"+ productType.getCreateTime()
                +"')";
        clearProductTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteProductType(int id) {
        String sql = "delete from product_type where id=" +id;
        clearProductTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int updateProductType(ProductTypeModel productType) {
        String sql = "update product_type set name='"+ productType.getName()
                +"',href='"+ productType.getHref()
                +"',update_time='"+ productType.getUpdateTime()
                +"' where id="+productType.getId();
        clearProductTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<ProductTypeModel> getProductTypeList(int page, int size) {
        String sql = "select * from product_type limit "+ (page-1)*size +","+size;
        return getProductTypeListResult(sql);
    }

    @Override
    public List<ProductTypeModel> getProductTypeList(int page, int size, String sort) {
        String sql = "select * from product_type order by "+ sort +" limit "+ (page-1)*size +","+size;
        return getProductTypeListResult(sql);
    }

    @Override
    public List<ProductTypeModel> searchProductType(String sql) {
        return getProductTypeListResult(sql);
    }

    @Override
    public long getProductTypeCount() {
        String sql = "select count(*) from product_type";
        return JDBCUtils.getCount(sql);
    }

    @Override
    public int updateEnable(int id, int enable) {
        String sql = "update product_type set enable="+enable+" where id="+id;
        clearProductTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<ProductTypeModel> getProductTypeListByRedis() {
        Jedis jedis = new Jedis();
        List<ProductTypeModel> list = new ArrayList<>();
        List<String> productTypeId = jedis.lrange("product_type_id",0,-1);
        for (int i = 0;i<productTypeId.size();i++){
            ProductTypeModel productType = new ProductTypeModel();
            productType.setId(Integer.valueOf(productTypeId.get(i)));
            productType.setName(jedis.lindex("product_type_name",i));
            productType.setHref(jedis.lindex("product_type_href",i));
//            productType.setCreateTime(jedis.lindex("product_type_create_time",i));
//            productType.setUpdateTime(jedis.lindex("product_type_update_time",i));
//            productType.setEnabled(Integer.valueOf(jedis.lindex("product_type_enable",i)));
//            productType.setNote(jedis.lindex("product_type_note",i));
            list.add(productType);
        }
        return list;
    }

    @Override
    public void setProductTypeInRedis(List<ProductTypeModel> list) {
        Jedis jedis = new Jedis();
        for (ProductTypeModel productType:list){
         jedis.rpush("product_type_id", String.valueOf(productType.getId()));
         jedis.rpush("product_type_name",productType.getName());
            jedis.rpush("product_type_href",productType.getHref());
//            jedis.rpush("product_type_create_time",productType.getCreateTime());
//            jedis.rpush("product_type_update_time",productType.getUpdateTime());
//            jedis.rpush("product_type_enable", String.valueOf(productType.getEnabled()));
//            jedis.rpush("product_type_note",productType.getNote());
        }
    }
}
